//
//  HJMinePresent.m
//  KaiChat
//
//  Created by lx on 2023/10/17.
//

#import "HJMinePresent.h"
#import "HJChatUserModel.h"
#import "HJIM.h"
#import "HJUserModel.h"
#import "HJRecaptchaClient.h"
#import <math.h>
#import "HJChatMessageModel.h"
#import "HJIM.h"
 

@implementation HJMinePresent

+ (void)loginWithAccount:(NSString *)account password:(NSString *)password complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:account forKey:@"email"];
    [parameters setValue:password forKey:@"password"];
    [parameters setValue:[self loginCommonParameters] forKey:@"common"];
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_loginV2 parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        HJUserInfo.email = account;
        [self loginCompleteWithData:data complete:complete];
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)appleLoginWithAppleUserId:(NSString *)appleUserId token:(NSString *)token complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:appleUserId forKey:@"apple_id"];
    [parameters setValue:token forKey:@"identity_token"];
    [parameters setValue:[self loginCommonParameters] forKey:@"common"];
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_appleLogin parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        [self loginCompleteWithData:data complete:complete];
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
    
}


+ (void)googleLoginWithGoogleUserId:(NSString *)googleUserId token:(NSString *)token email:(NSString *)email complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:googleUserId forKey:@"google_id"];
    [parameters setValue:token forKey:@"id_token"];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:[self loginCommonParameters] forKey:@"common"];
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_googleLogin parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        [self loginCompleteWithData:data complete:complete];
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
    
}

+ (NSMutableDictionary *)loginCommonParameters {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:@1 forKey:@"device_type"];
    [parameters setValue:getDeviceModelName() forKey:@"device_model"];
    [parameters setValue:getDeviceUDID() forKey:@"device_id"];
    [parameters setValue:getDeviceSystemVersion() forKey:@"sys_version"];
    [parameters setValue:getAppVersion() forKey:@"app_version"];
    [parameters setValue:HJTools.tool.longitude forKey:@"latitude"];
    [parameters setValue:HJTools.tool.longitude forKey:@"longitude"];
    if (HJTools.tool.inviteUserId) {
        [parameters setValue:@([HJTools.tool.inviteUserId longLongValue]) forKey:@"invite_userid"];
    }
    return parameters;
}


+ (void)emailCodeLoginWithEmail:(NSString *)email code:(NSString *)code complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:code forKey:@"code"];
    [parameters setValue:[self loginCommonParameters] forKey:@"common"];
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_emailCodeLogin parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        HJUserInfo.email = email;
        [self loginCompleteWithData:data complete:complete];
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)loginCompleteWithData:(NSDictionary *)data complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSDictionary *userInfo = data[@"user_info"];
    if (![userInfo isKindOfClass:NSDictionary.class]) {
        return;
    };
    
    NSString *userId = toString(userInfo[@"id"]);
    
    dispatch_async_sql_queue(^{
        
        HJUserModel *userModel = [HJUserModel modelWithUserId:userId];
        userModel.account = toString(userInfo[@"account"]);
        userModel.userId = toString(userInfo[@"id"]);
        userModel.oldUid = toString(userInfo[@"no"]);
        userModel.uid = toString(userInfo[@"new_no"]);
        userModel.avatar = userInfo[@"profile_picture"];
        userModel.nickname = userInfo[@"nickname"];
        userModel.whatsUp = toString(userInfo[@"brief"]);
        userModel.hasPassword = [userInfo[@"has_password"] boolValue];
        userModel.appleUserId = toString(userInfo[@"apple_id"]);
        userModel.googleUserId = toString(userInfo[@"google_id"]);
        userModel.googleEmail = toString(userInfo[@"google_email"]);
        userModel.refreshToken = data[@"refresh_token"];
        userModel.refreshExpire = toString(data[@"refresh_expire"]);
        [userModel bg_saveOrUpdate];
        
        dispatch_async_main_queue(^{
            
            HJUserInfo.isLogin = YES;
            HJUserInfo.userId = userModel.userId;
            HJUserInfo.no = userModel.uid;
            HJUserInfo.token = data[@"access_token"];
            HJUserInfo.refreshTokenDate = toString(data[@"access_expire"]);
            HJUserInfo.avatar = userModel.avatar;
            HJUserInfo.nickname = userModel.nickname;
            HJUserInfo.enableGray = [userInfo[@"enable_gray"] boolValue];

            if (isEmptyString(userModel.account)) {
                [USERDEFAULTS removeObjectForKey:user_email];
                [USERDEFAULTS removeObjectForKey:user_isRememberPassword];
                HJTools.tool.isRememberPassword = NO;
            } else {
                HJUserInfo.isRememberPassword = HJTools.tool.isRememberPassword;
            }
            
            !complete ?: complete(nil);
            
            [[NSNotificationCenter defaultCenter] postNotificationName:loginSucceedNotification object:nil];
            
            if ([data[@"is_first_login"] boolValue]) {
                [HJIM.shareIM.chatManager getFirstLoginReplyMessageCompletion:nil];
            }
            
            [self getPrivacySettingComplete:nil];
            [self getOutsideChainDataComplete:nil];
        });
    });
}

+ (void)getAccountStatus:(NSString *)email complete:(void (^__nullable)(NSInteger status, NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [HJNetworking.shareNetwork GET:API_accountStatus parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete([data[@"status"] intValue], nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(0, error);
    }];
}

+ (void)registerWithAccount:(NSString *)account password:(NSString *)password complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:account forKey:@"email"];
    [parameters setValue:password forKey:@"set_password"];
    [parameters setValue:password forKey:@"confirm_password"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_register parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        [[NSNotificationCenter defaultCenter] postNotificationName:registerSucceedNotification object:nil];
        !complete ?: complete(data);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}


+ (void)sendRegisterEmailCode:(NSString *)email complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:@"register" forKey:@"scene"];
    [self sendEmailCode:email parameters:parameters API:API_sendEmailCodeV2 complete:complete];
}

+ (void)sendLoginEmailCode:(NSString *)email complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:@"register" forKey:@"scene"];
    [self sendEmailCode:email parameters:parameters API:API_sendEmailCodeV2 complete:complete];
}

+ (void)sendResetPasswordEmailCode:(NSString *)email complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:@"reset" forKey:@"scene"];
    [self sendEmailCode:email parameters:parameters API:API_sendEmailCodeV2 complete:complete];
}

+ (void)sendPayPasswordEmailCode:(NSString *)email complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"account"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_KaiPaySetPasswordSendCode parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)sendBindingAccountWithEmailCode:(NSString *)email complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:@"bind" forKey:@"scene"];
    [self sendEmailCode:email parameters:parameters API:API_sendEmailCodeV2 complete:complete];
}

+ (void)bindingAccountWithEmail:(NSString *)email code:(NSString *)code complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:code forKey:@"code"];
    [self sendEmailCode:email parameters:parameters API:API_bindingEmail complete:complete];
}

+ (void)setPayPasswordWithEmail:(NSString *)email code:(NSString *)code password:(NSString *)password complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"account"];
    [parameters setValue:code forKey:@"code"];
    [parameters setValue:password forKey:@"new_password"];
    //    [parameters setValue:password forKey:@"old_password"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_KaiPaySetPassword parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}


+ (void)sendEmailCode:(NSString *)email parameters:(NSMutableDictionary *)parameters API:(NSString *)API complete:(void (^__nullable)(NSError * _Nullable error))complete {
    if (isHiddenAppleServe()) {
        [parameters setValue:@10 forKey:@"device_type"];
    } else {
        [parameters setValue:@1 forKey:@"device_type"];
    }
    [HJNetworking.shareNetwork POSTBodyJSON:API parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        if ([error.domain isEqualToString:ERRPRDOMAIN] && error.code == reCaptchaVerifyCode) {
            [self sendRecaptchaClientVerifyEmailCode:email parameters:parameters API:API complete:complete];
        } else {
            !complete ?: complete(error);
        }
    }];
}

+ (void)sendRecaptchaClientVerifyEmailCode:(NSString *)email parameters:(NSMutableDictionary *)parameters API:(NSString *)API complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    [HJRecaptchaClient recaptchaClientVerifyComplete:^(NSString * _Nullable token, NSError * _Nullable verifyError) {
        if (token) {
            [parameters setValue:token forKey:@"captcha_token"];
            [HJNetworking.shareNetwork POSTBodyJSON:API parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
                !complete ?: complete(nil);
            } failure:^(NSError * _Nonnull error) {
                !complete ?: complete(error);
            }];
        } else {
            !complete ?: complete(verifyError);
        }
    }];
}

+ (void)verifyRegisterCodeWithEmail:(NSString *)email code:(NSString *)code complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:@"register" forKey:@"scene"];
    [parameters setValue:code forKey:@"code"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_emailVerifyV2 parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)verifyResetPasswordCodeWithEmail:(NSString *)email code:(NSString *)code complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:@"reset" forKey:@"scene"];
    [parameters setValue:code forKey:@"code"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_emailVerifyV2 parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)verifyWalletOperationCodeWithEmail:(NSString *)email code:(NSString *)code complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:@"wallet" forKey:@"type"];
    [parameters setValue:code forKey:@"code"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_emailVerify parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
        
        
    }];
}

+ (void)setPasswordWithEmail:(NSString *)email password:(NSString *)password code:(NSString *)code complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:code forKey:@"code"];
    [parameters setValue:password forKey:@"set_password"];
    [parameters setValue:password forKey:@"confirm_password"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_setPassword parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)resetPasswordWithEmail:(NSString *)email password:(NSString *)password code:(NSString *)code complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:code forKey:@"code"];
    [parameters setValue:password forKey:@"set_password"];
    [parameters setValue:password forKey:@"confirm_password"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_resetPassword parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)refreshTokenComplete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSInteger refreshTokenDate = HJUserInfo.refreshTokenDate.integerValue;
    NSTimeInterval currentDate = [NSDate date].timeIntervalSince1970;
    
    if (refreshTokenDate > currentDate) {
        TJLog(@"token未过期");
        !complete ?: complete(nil);
        return;
    }
    
    dispatch_async_sql_queue(^{
        HJUserModel *userModel = [HJUserModel currentAccountModel];
        if (userModel.refreshToken && userModel.refreshExpire.integerValue > currentDate) {
            [HJNetworking.shareNetwork POSTBodyJSON:API_refreshToken parameters:@{
                @"refresh_token": userModel.refreshToken
            } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
                HJUserInfo.token = data[@"access_token"];
                HJUserInfo.refreshTokenDate = toString(data[@"access_expire"]);
                !complete ?: complete(nil);
                
                dispatch_async_sql_queue(^{
                    HJUserModel *userModel = [HJUserModel modelWithUserId:HJUserInfo.userId];
                    userModel.refreshToken = data[@"refresh_token"];
                    userModel.refreshExpire = toString(data[@"refresh_expire"]);
                    [userModel bg_saveOrUpdate];
                });
                TJLog(@"刷新token成功");
            } failure:^(NSError * _Nonnull error) {
                !complete ?: complete(error);
            }];
        } else {
            dispatch_async_main_queue(^{
                !complete ?: complete(nil);
            });
        }
    });
    
}

+ (void)logoutComplete:(void (^__nullable)(NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_logOut parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)revokedComplete:(void (^__nullable)(NSError * _Nullable error, NSString *message))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_deleteAccount parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        HJUserInfo.email = nil;
        [HJUserInfo clear];
        [[NSNotificationCenter defaultCenter] postNotificationName:cancellationSucceedNotification object:nil];
        !complete ?: complete(nil, responseObject[@"msg"]);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error, nil);
    }];
}

+ (void)unfreezeWithEmail:(NSString *)email password:(NSString *)password  complete:(void (^__nullable)(NSError * _Nullable error))complete {
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:email forKey:@"email"];
    [parameters setValue:password forKey:@"password"];
    [HJNetworking.shareNetwork POSTBodyJSON:API_unfreeze parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)getAgreementComplete:(void (^__nullable)(NSString * _Nullable url))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_user_agree parameters:@{
        @"agree_name": @"user_agreement"
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        NSString *agreement = data[@"user_agreement"];
        HJUserInfo.agreement = agreement;
        !complete ?: complete(agreement);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
}

+ (void)loadVersionInfoComplete:(void (^__nullable)(NSDictionary * _Nullable data, NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork GET:API_versionInfo parameters:@{
        @"type": @"2",
        @"channel": getAppChannel()
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        NSDictionary *app_version = data[@"app_version"];
        if ([app_version isKindOfClass:NSDictionary.class]) {
            !complete ?: complete(app_version, nil);
        } else {
            !complete ?: complete(nil,[NSError errorWithCode:-1001 errorReason:@""]);
        }
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}

+ (void)loadVersionListWithPageIndex:(NSInteger)pageIndex pageSize:(NSInteger)pageSize complete:(void (^__nullable)(NSArray * _Nullable data, NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork GET:API_versionList parameters:@{
        @"type": @"2",
        @"channel": getAppChannel(),
        @"page_no": @(pageIndex),
        @"page_size": @(pageSize)
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(data[@"list"], nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}

+ (void)loadUserInfoComplete:(void (^__nullable)(HJUserModel * _Nullable userModel))complete {
    
    
    void  (^success)(NSDictionary * _Nonnull responseObject, id  _Nonnull data) = ^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        NSDictionary *userInfo = data[@"user_info"];
        NSString *userId = toString(userInfo[@"id"]);
        NSString *account = userInfo[@"account"];
        
        dispatch_async_sql_queue(^{
            
            HJChatUserModel *chatUserModel = [HJChatUserModel modelWithBlock:^(HJChatUserModel * _Nonnull model) {
                model.userId = toString(userInfo[@"id"]);
                model.brief = userInfo[@"brief"];
                model.avatar = userInfo[@"profile_picture"];
                model.nickname = userInfo[@"nickname"];
                model.gender = [userInfo[@"gender"] intValue];
                model.uid = userInfo[@"new_no"];
                model.status = [userInfo[@"status"] intValue];
                model.isCanModify = [data[@"is_can_modify_no"] boolValue];
            }];
            [chatUserModel bg_saveOrUpdate];
            
            HJUserModel *userModel = [HJUserModel modelWithUserId:userId];
            userModel.account = account;
            userModel.userId = toString(userInfo[@"id"]);
            userModel.uid = toString(userInfo[@"new_no"]);
            userModel.oldUid = toString(userInfo[@"no"]);
            userModel.avatar = userInfo[@"profile_picture"];
            userModel.nickname = userInfo[@"nickname"];
            userModel.whatsUp = toString(userInfo[@"brief"]);
            userModel.birthday = toString(userInfo[@"birth"]);
            userModel.region = toString(userInfo[@"region"]);
            userModel.gender = toString(userInfo[@"gender"]);
            userModel.isCanModify = [data[@"is_can_modify_no"] boolValue];
            userModel.modifyDate = toString(data[@"can_modify_no_date"]);
            userModel.hasPassword = [userInfo[@"has_password"] boolValue];
            userModel.appleUserId = toString(userInfo[@"apple_id"]);
            userModel.googleUserId = toString(userInfo[@"google_id"]);
            userModel.googleEmail = toString(userInfo[@"google_email"]);
            [userModel bg_saveOrUpdate];
            
            dispatch_async_main_queue(^{
                if (isEmptyString(userModel.account)) {
                    [USERDEFAULTS removeObjectForKey:user_email];
                    [USERDEFAULTS removeObjectForKey:user_isRememberPassword];
                    HJTools.tool.isRememberPassword = NO;
                } else {
                    HJUserInfo.email = userModel.account;
                    HJUserInfo.isRememberPassword = HJTools.tool.isRememberPassword;
                }
                
                HJUserInfo.userId = userModel.userId;
                HJUserInfo.no = userModel.uid;
                HJUserInfo.avatar = userModel.avatar;
                HJUserInfo.nickname = userModel.nickname;
                !complete ?: complete(userModel);
                
                HJTools.tool.userModel = userModel;
            });
            
        });
    };
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_selfInfoV2 parameters:nil success:success failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
    
}

+ (void)revokeCheckComplete:(void (^__nullable)(BOOL res))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_revokedCheck parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(YES);
    } failure:^(NSError * _Nonnull error) {
        if ([error.domain isEqualToString:ERRPRDOMAIN]) {
            !complete ?: complete(NO);
        }
    }];
}

+ (void)loginCheckComplete:(void (^__nullable)(BOOL res, NSString *msg))complete {
    
    if (isDevelopment()) {
        return;
    }
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_loginIpCheck parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(YES, @"");
    } failure:^(NSError * _Nonnull error) {
        if ([error.domain isEqualToString:ERRPRDOMAIN]) {
            !complete ?: complete(NO, error.localizedFailureReason);
        }
    }];
}

+ (void)loadReportTypeListDataComplete:(void (^__nullable)(NSArray * _Nullable datas))complete {
    [HJNetworking.shareNetwork GET:API_reportTypeList parameters:@{
        @"scene": @"Report"
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(data[@"list"]);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
}


+ (void)reportFridend:(NSString *)userId type:(NSString *)type detail:(NSString *)detail complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setValue:userId forKey:@"friend_id"];
    [parameters setValue:type forKey:@"type"];
    [parameters setValue:detail forKey:@"detail"];
    [parameters setValue:@"Report" forKey:@"scene"];
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_report parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)loadBlacklistListDataComplete:(void (^__nullable)(NSArray <HJChatBlacklistModel *>* _Nullable datas, HJLoadType loadType))complete {
    
    //    [HJChatBlacklistModel bg_findAsync:[HJChatBlacklistModel bg_tableName] where:[NSString stringWithFormat:@"where %@=%@", bg_sqlKey(@"account"), bg_sqlValue(HJUserInfo.userId)] complete:^(NSArray * _Nullable array) {
    //        if (array.count && complete) {
    //            dispatch_async_main_queue(^{
    //                complete(array, HJLoadTypeCache);
    //            });
    //        };
    //    }];
    //
    //
    
    [HJNetworking.shareNetwork GET:API_blacklistListV2 parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        NSArray *list = data[@"list"];
        NSMutableArray *userIds = [NSMutableArray array];
        NSMutableArray *datas = [NSMutableArray arrayWithCapacity:list.count];
        
        for (NSDictionary *dict in list) {
            HJChatBlacklistModel *model = [HJChatBlacklistModel modelWithDict:dict];
            [userIds addObject:model.userId];
            [datas addObject:model];
        }
        !complete ?: complete(datas, HJLoadTypeNetwork);
        
        //        [HJChatBlacklistModel bg_findAsync:[HJChatBlacklistModel bg_tableName] where:[NSString stringWithFormat:@"where %@=%@", bg_sqlKey(@"account"), bg_sqlValue(HJUserInfo.userId)] complete:^(NSArray * _Nullable array) {
        //
        //            [array enumerateObjectsUsingBlock:^(HJChatFriendModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        //                if (![userIds containsObject:obj.userId]) {
        //                    [HJChatBlacklistModel bg_delete:[HJChatFriendModel bg_tableName] where:[NSString stringWithFormat:@"where %@=%@ and %@=%@", bg_sqlKey(@"account"), bg_sqlValue(HJUserInfo.userId), bg_sqlKey(@"userId"),bg_sqlValue(obj.userId)]];
        //                }
        //            }];
        //            [HJChatBlacklistModel bg_saveOrUpdateArray:datas];
        //        }];
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, HJLoadTypeNetwork);
    }];
}

+ (void)setBacklistWithUserId:(NSString *)userId backlist:(BOOL)backlist complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_setBlacklist parameters:@{
        @"friend_id": userId,
        @"option": backlist ? @1: @0
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        dispatch_async_sql_queue(^{
            
            HJChatUserModel *userModel = [HJIM.shareIM.userManager queryUserInfoByUserId:userId];
            HJChatFriendModel *friendModel = [HJIM.shareIM.userManager queryFriendInfoByFriendId:userId];
            
            if (backlist) {
                userModel.relationship = 4;
                friendModel.status = 2;
            } else {
                userModel.relationship = 1;
                friendModel.status = 1;
            }
            
            [userModel bg_saveOrUpdate];
            [friendModel bg_saveOrUpdate];
        });
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)setBacklistWithUserIds:(NSArray *)userIds backlist:(BOOL)backlist complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSString *API = backlist ? API_addBlacklist : API_removeBlacklist;
    [HJNetworking.shareNetwork POSTBodyJSON:API parameters:@{
        @"friend_ids": userIds,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        dispatch_async_sql_queue(^{
            
            for (NSString *userId in userIds) {
                HJChatUserModel *userModel = [HJIM.shareIM.userManager queryUserInfoByUserId:userId];
                HJChatFriendModel *friendModel = [HJIM.shareIM.userManager queryFriendInfoByFriendId:userId];
                
                if (backlist) {
                    userModel.relationship = 4;
                    friendModel.status = 2;
                } else {
                    userModel.relationship = 1;
                    friendModel.status = 1;
                }
                
                [userModel bg_saveOrUpdate];
                [friendModel bg_saveOrUpdate];
            }
        });
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)modifyInfoWithKey:(NSString *)key value:(NSString *)value complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSMutableArray *datas = [NSMutableArray array];
    [datas addObject:@{
        @"edit_field": key,
        @"edit_value": value
    }];
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_editInfoV2 parameters:@{
        @"edit_info": datas,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
    
}

+ (void)modifyNickname:(NSString *)nickname complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [self modifyInfoWithKey:@"nickname" value:nickname complete:complete];
}

+ (void)modifyAvatar:(NSString *)avatar complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [self modifyInfoWithKey:@"profile_picture" value:avatar complete:complete];
}

+ (void)modifyGender:(NSString *)gender complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [self modifyInfoWithKey:@"gender" value:gender complete:complete];
}

+ (void)modifyWhatsUp:(NSString *)whatsUp complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [self modifyInfoWithKey:@"brief" value:whatsUp complete:complete];
}

+ (void)modifyUID:(NSString *)UID complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [self modifyInfoWithKey:@"new_no" value:UID complete:complete];
}

+ (void)modifyBirth:(NSString *)birth complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [self modifyInfoWithKey:@"birth" value:birth complete:complete];
}

+ (void)modifyRegion:(NSString *)region complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [self modifyInfoWithKey:@"region" value:region complete:complete];
}

+ (void)getRegionWithLongitude:(NSString *)longitude latitude:(NSString *)latitude complete:(void (^__nullable)(NSString * _Nullable region))complete {
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_region parameters:@{
        @"longitude": longitude,
        @"latitude": latitude
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(data[@"region"]);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
}

+ (void)modifyProfile:(nullable NSString *)profile nickname:(nullable NSString *)nickname complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSMutableArray *datas = [NSMutableArray array];
    if (profile) {
        [datas addObject:@{
            @"edit_field": @"brief",
            @"edit_value": profile
        }];
    }
    
    if (nickname) {
        [datas addObject:@{
            @"edit_field": @"nickname",
            @"edit_value": nickname
        }];
    }
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_editInfoV2 parameters:@{
        @"edit_info": datas,
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
    
}

+ (void)bindGoogle:(NSString *)email token:(NSString *)token complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_bindGoogle parameters:@{
        @"email": email,
        @"device_type": @1,
        @"id_token": token
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)bindApple:(NSString *)appleUserId token:(NSString *)token complete:(void (^__nullable)(NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_bindApple parameters:@{
        @"apple_id": appleUserId,
        @"identity_token": token
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
    
}

+ (void)getNoticeConfigComplete:(void (^__nullable)(NSError * _Nullable error))complete {
    return;
    [HJNetworking.shareNetwork POSTBodyJSON:API_getNoticeSetting parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        dispatch_async_sql_queue(^{
            HJUserModel *userModel = [HJUserModel currentAccountModel];
            NSArray *notice_sound_list = data[@"notice_sound_list"];
            for (NSDictionary *dict in notice_sound_list) {
                if ([dict[@"notice_type"] intValue] == HJNoticeSettingTypeIsOpen) {
                    userModel.noticeOpen = [dict[@"notice_status"] intValue] == 1;
                } else if ([dict[@"notice_type"] intValue] == HJNoticeSettingTypeSound) {
                    userModel.noticeSound = [dict[@"notice_status"] intValue] == 1;
                } else if ([dict[@"notice_type"] intValue] == HJNoticeSettingTypePreview) {
                    userModel.noticePreview = [dict[@"notice_status"] intValue] == 1;
                } else if ([dict[@"notice_type"] intValue] == HJNoticeSettingTypeVibration) {
                    userModel.noticeVibration = [dict[@"notice_status"] intValue] == 1;
                }
            }
            [userModel bg_saveOrUpdate];
            
            if (complete) {
                dispatch_async_main_queue(^{
                    complete(nil);
                });
            }
        });
        
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)setNoticeConfig:(HJNoticeSettingType)type value:(BOOL)value complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    dispatch_async_sql_queue(^{
        HJUserModel *userModel = [HJUserModel currentAccountModel];
        if (type == HJNoticeSettingTypeIsOpen) {
            userModel.noticeOpen = value;
        } else if (type == HJNoticeSettingTypePreview) {
            userModel.noticePreview = value;
        } else if (type == HJNoticeSettingTypeSound) {
            userModel.noticeSound = value;
        } else if (type == HJNoticeSettingTypeVibration) {
            userModel.noticeVibration = value;
        }
        [userModel bg_saveOrUpdate];
        
        dispatch_async_main_queue(^{
            !complete ?: complete(nil);
        });
    });
    
}

+ (void)resetNoticeConfigComplete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    dispatch_async_sql_queue(^{
        HJUserModel *userModel = [HJUserModel currentAccountModel];
        userModel.noticeOpen = YES;
        userModel.noticePreview = YES;
        userModel.noticeSound = YES;
        userModel.noticeVibration = YES;
        [userModel bg_saveOrUpdate];
        
        dispatch_async_main_queue(^{
            !complete ?: complete(nil);
        });
    });
}

+ (void)getPrivacySettingComplete:(void (^__nullable)(NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork GET:API_getPrivacySetting parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        dispatch_async_sql_queue(^{
            HJUserModel *userModel = [HJUserModel currentAccountModel];
            userModel.bandAdd = [data[@"ban_add"] intValue] == 2;
            userModel.UIDAdd = [data[@"ban_uid_add"] intValue] == 1;
            userModel.emailAdd = [data[@"ban_email_add"] intValue] == 1;
            [userModel bg_saveOrUpdate];
        });
        !complete ?: complete(nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)privacySettingWithWay:(HJAddWay)way value:(BOOL)value complete:(void (^__nullable)(NSError * _Nullable error))complete {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    if (way == HJAddWayBan) {
        [parameters setValue:@{@"ban_add" : value ? @2 : @1} forKey:@"friend_add"];
    } else if (way == HJAddWayUID) {
        [parameters setValue:@{@"ban_uid_add" : value ? @1 : @2} forKey:@"friend_add"];
    } else if (way == HJAddWayEmail) {
        [parameters setValue:@{@"ban_email_add" : value ? @1 : @2} forKey:@"friend_add"];
    }
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_privacySetting parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        !complete ?: complete(nil);
        
        dispatch_async_sql_queue(^{
            
            HJUserModel *userModel = [HJUserModel currentAccountModel];
            if (way == HJAddWayUID) {
                userModel.UIDAdd = value;
            } else if (way == HJAddWayEmail) {
                userModel.emailAdd = value;
            } else if (way == HJAddWayBan) {
                userModel.bandAdd = value;
            }
            [userModel bg_saveOrUpdate];
        });
        
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(error);
    }];
}

+ (void)getKaiPayBalanceComplete:(void (^__nullable)(NSString *_Nullable balance,  NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork GET:API_KaiPayBalance parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        NSString *balance = toString(data[@"balance"]);
        !complete ?: complete(balance, nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}

+ (void)kaiearmIsComplete:(void (^__nullable)(BOOL res, NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_KaiearmComplete parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        BOOL res = ![data[@"has_un_settled_order"] boolValue];
        !complete ?: complete(res,  nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(NO, error);
    }];
}

+ (void)MPCIsAbleDestroyedComplete:(void (^__nullable)(BOOL res, NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork GET:API_MPCWalletList parameters:@{
        @"wallet_id": @1
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        
        BOOL able = YES;
        for (NSDictionary *dict in data[@"list"]) {
            if ([dict[@"import_type"] intValue] == 3 && [dict[@"status"] intValue] == 2) {
                able = NO;
                break;
            }
        }
        
        !complete ?: complete(able,  nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(NO, error);
    }];
}

+ (void)getMPCWalletAddressComplete:(void (^__nullable)(NSString * _Nullable address, NSError * _Nullable error))complete {
    
    [HJNetworking.shareNetwork GET:API_MPCWalletList parameters:@{
        @"wallet_id": @1
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        NSString *public_key = nil;
        for (NSDictionary *dict in data[@"list"]) {
            if ([dict[@"import_type"] intValue] == 3 && [dict[@"status"] intValue] == 2) {
                public_key = dict[@"public_key"];
                break;
            }
        }
        if (public_key) {
            [self publicKeyToAddress:public_key complete:complete];
        } else {
            HJUserInfo.MPCWalletAddress = @"";
            !complete ?: complete(nil, [NSError errorWithCode:-10010]);
        }
    } failure:^(NSError * _Nonnull error) {
        HJUserInfo.MPCWalletAddress = @"";
        !complete ?: complete(nil, error);
    }];
}

+ (void)publicKeyToAddress:(NSString *)publicKey complete:(void (^__nullable)(NSString * _Nullable address, NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_publicKeyToAddress parameters:@{
        @"data": @[
            @{
                @"type": @"erc20",
                @"public_key": publicKey
            }
        ]
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        NSString *address = nil;
        for (NSDictionary *dict in data[@"address_list"]) {
            address = dict[@"address"];
            if (!isEmptyString(address)){
                break;
            }
        }
        !complete ?: complete(address,  nil);
    } failure:^(NSError * _Nonnull error) {
        HJUserInfo.MPCWalletAddress = @"";
        !complete ?: complete(nil, error);
    }];
}



+ (void)prepareOrderWithFromUserId:(NSString *)fromUserId toUserId:(NSString *)toUserId value:(NSString *)value complete:(void (^__nullable)(NSString * _Nullable orderNo, NSError * _Nullable error))complete {
    
    NSLog(@"prepareOrderWithFromUserId = %@", toUserId);
    
    if (!toUserId) {
        !complete ?: complete(nil, [NSError errorWithCode:-1 errorReason:@"toUserId error"]);
        return;
    };
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_KaiPayPrepareOrder parameters:@{
        @"from_uid": fromUserId,
        @"to_uid": toUserId,
        @"value": Multiplying_powl(value, 10, 6),
        @"pay_type": @1,
        @"order_type": @1
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(data[@"order_no"],  nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}

+ (void)payWithOrderId:(NSString *)orderId password:(NSString *)password complete:(void (^__nullable)(NSDictionary * _Nullable orderInfo, NSError * _Nullable error))complete {
    
    [HJNetworking.shareNetwork POSTBodyJSON:API_KaiPayOrderPayment parameters:@{
        @"order_no": orderId,
        @"password": password,
        @"type": @1
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(data[@"orderInfo"],  nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}


+ (void)isSetPayPasswordComplete:(void (^__nullable)(BOOL res, NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork GET:API_KaiPayIsSetPassword parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete([data[@"has_password"] boolValue],  nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}

+ (void)getOrderDetail:(NSString *)orderId Complete:(void (^__nullable)(NSDictionary * _Nullable orderInfo, NSError * _Nullable error))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_KaiPayOrderDetail parameters:@{
        @"order_no": orderId
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete(data[@"balance"],  nil);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil, error);
    }];
}

+ (void)getOutsideChainDataComplete:(void (^__nullable)(NSArray <HJCellModel *> * _Nullable array))complete {
    
    NSArray *list = [[NSUserDefaults standardUserDefaults] objectForKey:outsideChainKey];
    if (list.count) {
        [self buildOutsideChainData:list complete:complete];
        return;
    }
    
    [HJNetworking.shareNetwork GET:API_outsideChain parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        if ([data isKindOfClass:NSDictionary.class] && [data[@"list"] isKindOfClass:NSArray.class]) {
            NSArray *list = data[@"list"];
            [[NSUserDefaults standardUserDefaults] setObject:list forKey:outsideChainKey];
            [self buildOutsideChainData:list complete:complete];
        } else {
            !complete ?: complete(nil);
        }
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
}

+ (void)loadOutsideChainDataComplete:(void (^__nullable)(NSArray <HJCellModel *> * _Nullable array))complete {
    [HJNetworking.shareNetwork GET:API_outsideChain parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        if ([data isKindOfClass:NSDictionary.class] && [data[@"list"] isKindOfClass:NSArray.class]) {
            NSArray *list = data[@"list"];
            [[NSUserDefaults standardUserDefaults] setObject:list forKey:outsideChainKey];
            [self buildOutsideChainData:list complete:complete];
        } else {
            !complete ?: complete(nil);
        }
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(nil);
    }];
}


+ (void)integraWaitingReceiveComplete:(void (^__nullable)(BOOL isReceive))complete {
    [HJNetworking.shareNetwork POSTBodyJSON:API_unclaimedScore parameters:@{
        @"task_type": @5
    } success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        !complete ?: complete([data[@"score"] intValue] > 0);
    } failure:^(NSError * _Nonnull error) {
        !complete ?: complete(NO);
    }];
}

+ (void)buildOutsideChainData:(NSArray *)list complete:(void (^__nullable)(NSArray <HJCellModel *> * _Nullable array))complete {
    
    NSMutableArray *datas = [NSMutableArray array];
    for (NSDictionary *dict in list) {
        
        NSString *user_name = dict[@"user_name"];
        NSString *tag = dict[@"tag"];
        
        if ([tag isEqualToString:@"twitter" options:NSCaseInsensitiveSearch]) {
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"X_icon";
                model.identifier = @"X";
                model.textValue = [NSString stringWithFormat:@"https://twitter.com/%@", user_name];
                model.detailText = [NSString stringWithFormat:@"twitter://user?screen_name=%@", user_name];
                [datas addObject:model];
            }];
        } else if ([tag isEqualToString:@"linkedin" options:NSCaseInsensitiveSearch]) {
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"linkedin_icon";
                model.identifier = @"Linkedin";
                model.textValue = [NSString stringWithFormat:@"https://www.linkedin.com/in/%@/", user_name];
                model.detailText = [NSString stringWithFormat:@"linkedin://profile/%@", user_name];
                [datas addObject:model];
            }];
        } else if ([tag isEqualToString:@"discord" options:NSCaseInsensitiveSearch]) {
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"discord_icon";
                model.identifier = @"discord";
                model.textValue = [NSString stringWithFormat:@"https://discord.com/invite/%@", user_name];
                model.detailText = [NSString stringWithFormat:@"discord://invite/%@", user_name];
                [datas addObject:model];
            }];
        } else if ([tag isEqualToString:@"instagram" options:NSCaseInsensitiveSearch]) {
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"instagram_icon";
                model.identifier = @"instagram";
                model.textValue = [NSString stringWithFormat:@"https://www.instagram.com/%@/", user_name];
                model.detailText = [NSString stringWithFormat:@"instagram://user?username=%@", user_name];
                [datas addObject:model];
            }];
        } else if ([tag isEqualToString:@"telegram" options:NSCaseInsensitiveSearch]) {
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"telegram_icon";
                model.identifier = @"telegram";
                model.textValue = [NSString stringWithFormat:@"https://t.me/%@", user_name];
                model.detailText = [NSString stringWithFormat:@"tg://resolve?domain=%@", user_name];
                [datas addObject:model];
            }];
        } else if ([tag isEqualToString:@"facebook" options:NSCaseInsensitiveSearch]) {
            [HJCellModel modelWithBlock:^(HJCellModel * _Nonnull model) {
                model.iconName = @"facebook_icon";
                model.identifier = @"facebook";
                model.textValue = [NSString stringWithFormat:@"https://www.facebook.com/profile.php?id=%@", user_name];
                model.detailText = [NSString stringWithFormat:@"fb://profile/%@", user_name];
                [datas addObject:model];
            }];
        }
    }
    !complete ?: complete(datas);
}


+ (void)loadDappBookmarkListComplete:(void (^__nullable)(NSArray *array))complete {
    
    dispatch_async_sql_queue(^{
        
      NSArray *dAppList = [NSDictionary bg_valueForKey:DAppListKey];
        if (dAppList.count) {
            dispatch_async_main_queue(^{
                !complete ?: complete(dAppList);
            });
        }
        
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        if (getAppEnvType() == HJAppEnvTypePro) {
            [parameters setObject:@1 forKey:@"type"];
            [parameters setObject:@1 forKey:@"id"];
        } else {
            [parameters setObject:@0 forKey:@"type"];
            [parameters setObject:@9 forKey:@"id"];
        }
        
        [HJNetworking.shareNetwork POSTBodyJSON:API_dappBookmark parameters:parameters success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
                    
            NSMutableArray *datas = [NSMutableArray array];
            NSDictionary *firstDict = [data[@"bookmarkList"] firstObject];
            for (NSDictionary *dict in firstDict[@"children"]) {
                if (dict[@"bookmark"]) {
                    [datas addObject:dict[@"bookmark"]];
                }
            }
            !complete ?: complete(datas);
            
            if (datas.count) {
                dispatch_async_sql_queue(^{
                    [NSDictionary bg_setValue:datas forKey:DAppListKey];
                });
            }
        } failure:^(NSError * _Nonnull error) {
           
        }];
    });
}

+ (void)loadChainConfigComplete:(void (^__nullable)(NSArray *array))complete {
    return;
    [HJNetworking.shareNetwork GET:API_getChainConfig parameters:nil success:^(NSDictionary * _Nonnull responseObject, id  _Nonnull data) {
        [USERDEFAULTS setObject:data forKey:chainConfigKey];
    } failure:^(NSError * _Nonnull error) {}];
}


@end



